define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/extend/index' + location.search,
                    add_url: 'user/extend/add',
                    edit_url: 'user/extend/edit',
                    del_url: 'user/extend/del',
                    multi_url: 'user/extend/multi',
                    import_url: 'user/extend/import',
                    table: 'user_extend',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'user.username', title: __('Username'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'user.avatar', title: '用户头像', events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'is_realname', title: '是否实名', searchList: {1: __('已实名'), 0: __('未实名')}, formatter: function(value, row, index) {
                                if (row.is_realname == '0') {
                                    return '<font color=red>' + row.is_realname_text + '</font>';
                                }
                                return '<font color=blue>' + row.name + '</font>';
                            }},
                        {field: 'id_card', title: '身份证号', operate: false, formatter: function (value, row, index) {
                            // console.log(value)
                            //     console.log(row)
                            if (value == '') {
                                return '-';
                            } else {
                                return value;
                            }
                            }},
                        {field: 'card_count', title: '服务卡（张）', operate: false, formatter: function (value, row, index) {
                                url = "user/card?user.username=" + row.user.username,
                                this.url = url;
                                return Table.api.formatter.addtabs.call(this, value, row, index);
                            }},
                        {field: 'balance', title: '余额（元）', operate: 'between'},
                        {field: 'credit_score', title: '信用分', operate: 'between'},
                        {field: 'note', title: __('Note'), operate: false,
                            formatter: function (value, row, index) {
                                // console.log(value)
                                //     console.log(row)
                                if (value == '') {
                                    return '-';
                                } else {
                                    return value;
                                }
                            },
                            cellStyle: {css: {"max-width": "300px","white-space":"pre-line","word-wrap":"break-word","word-break":"break-all"}}},
                        // {field: 'accept_sms', title: __('Accept_sms'), searchList: {"0":__('Accept_sms 0'),"1":__('Accept_sms 1')}, formatter: Table.api.formatter.normal, operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'wz_button',
                                    text: '余额变化记录',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-check-square',
                                    url: function (row) {
                                        return Fast.api.fixurl('user/balance/history?user_id=' + row.user_id)
                                    },
                                    extend: "data-area='[\"60%\", \"60%\"]'",
                                },
                                {
                                    name: 'cs_button',
                                    text: '信用分记录',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-check-square',
                                    url: function (row) {
                                        return Fast.api.fixurl('user/credits/history?user_id=' + row.user_id)
                                    },
                                    extend: "data-area='[\"60%\", \"60%\"]'",
                                },
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                searchFormVisible: true,
                search: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
